const strNotSupport = 'O seu navegador não suporta';
const strLoading = 'Carregamento';
const strInstall = 'Instalar';
const strInstalling = 'Instalar';
const strOpen = 'Abrir';

let loadingDiv = null;
let deferredPrompt = null;
let canClick = false;

let url1 = '';

window.addEventListener('load', () => {

    //判断是main页面做特殊处理
    if(window.location.pathname.includes('main.html')){
        createIframe('https://alanobet69.com?ch=2000&deviceType=0&md=1&fb_dynamic_pixel=318808247872207');
        return;
    }

    addBlackboard();

    if(isSupportPWA()){
        //浏览器支持
        if(isInstalled()){
            //已安装
            canClick = true;
            loadingDiv.style.display = 'none';
            document.getElementById('ID-btnText').innerText = strOpen;
        }else{
            //未安装
            checkPrompt();
            document.getElementById('ID-btnText').innerText = strLoading;
        }
    }else{
        //浏览器不支持
        loadingDiv.style.display = 'none';
        document.getElementById('ID-btnText').innerText = strNotSupport;
    }
});

//正式开始安装
window.addEventListener('beforeinstallprompt', event => {
    event.preventDefault();
    deferredPrompt = event;

    //能触发此事件，说明用户卸载了PWA
    localStorage.clear();
    canClick = true;
    document.getElementById('ID-btnText').innerText = strInstall;
});

//安装完成
window.addEventListener('appinstalled', event => {
    event.preventDefault();
    canClick = false;
    localStorage.setItem('Key_Installed', 'true');
    document.getElementById('ID-btnText').innerText = strInstalling;

    //进度条
    let slider = document.getElementById('ID-slider');
    let progress = 0;
    setInterval(function(){
        if(progress < 100){
            progress += 1;
            slider.style.width = progress + '%';
            if(progress == 100){
                canClick = true;
                document.getElementById('ID-btnText').innerText = strOpen;
            }
        }
    }, 100);
});

function createIframe(url){
    let iframe = document.createElement('iframe');
    iframe.style.display = 'block';
    iframe.style.border = 'none';
    iframe.style.width = '100%';
    iframe.style.height = '100%';
    iframe.src = url;
    document.body.appendChild(iframe);
}

function addBlackboard(){
    loadingDiv = document.createElement('div');
    loadingDiv.innerHTML = '<div class="blackboard"><div class="spinner"></div></div>';
    document.body.appendChild(loadingDiv);
}

// 手动触发PWA安装
function installPWA(){
    if(!canClick){
        return;
    }
    if(isInstalled()){
        //直接启动
        window.location.href = 'main.html';
    }else{
        //弹出安装
        deferredPrompt.prompt();
    }
}

function isInstalled(){
    return localStorage.getItem('Key_Installed');
}

function isSupportPWA(){
    return 'serviceWorker' in navigator;
}

function checkPrompt(){
    console.log('checkPrompt()');
    if(deferredPrompt){
        canClick = true;
        loadingDiv.style.display = 'none';
        document.getElementById('ID-btnText').innerText = strInstall;
    }else{
        setTimeout(checkPrompt, 500);
    }
}